define('utils/debounce',[], function() {
    function debounce(func, wait, immediate) {
        var time
        var debounced = function() {
            var context = this
            if(time) clearTimeout(time)
    
            if(immediate) {
                var callNow = !time
                if(callNow) func.apply(context, arguments)
                time = setTimeout(
                    function(){time = null} //见注解
                , wait)
            } else {
                time = setTimeout(
                    function(){func.apply(context, arguments)}
                , wait) 
            }
        }
    
        debounced.cancel = function() {
            clearTimeout(time)
            time = null
        }
    
        return debounced
    }
    
    return debounce
});
